package com.zhiyume.controller;

import com.zhiyume.dao.entity.TVersion;
import com.zhiyume.page.PageReq;
import com.zhiyume.service.TVersionService;
import com.zhiyume.service.common.ConstantEnum;
import com.zhiyume.service.util.JsonUtil;
import com.zhiyume.shiro.AdminSecurityManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 * @author wang
 * @version $Id: VersionController.java, v 0.1 2018-08-07 18:59:44 wang Exp $$
 */
@Controller
@RequestMapping("/version")
public class VersionController {
    @Autowired
    private TVersionService tVersionService;

    /**
     * 根据Id查询信息
     * @return
     */
    @RequestMapping("/findById")
    @ResponseBody
    public Map findById(long id){
        TVersion version = tVersionService.findVersionById(id);

        if(null == version.getId()){
           return JsonUtil.returnResult(ConstantEnum.CONSTANT_MISS_PARA.getErrType(),ConstantEnum.CONSTANT_MISS_PARA.getErrMsg());
        }
        return JsonUtil.returnResult(ConstantEnum.CONSTANT_SUCCESS.getErrType(),ConstantEnum.CONSTANT_SUCCESS.getErrMsg(),version);
    }

    /**
     * 添加信息
     * @param tVersion     * @return
     */
    @RequestMapping("/addVersion")
    @ResponseBody
    public Map addVersion(TVersion tVersion) {

        TVersion version = new TVersion();
        
        Long sessionId = AdminSecurityManager.getSessionTAdminSys().getId();
        tVersion.setCreateId(sessionId);
        tVersion.setUpdateId(sessionId);
        String errType =ConstantEnum.CONSTANT_SUCCESS.getErrType();
        String errMsg =ConstantEnum.CONSTANT_SUCCESS.getErrMsg(); 
        try{
            version = tVersionService.addVersion(tVersion);
        }catch (Exception e){
            errType =ConstantEnum.CONSTANT_SYS_ERROR.getErrType();
            errMsg =e.getMessage();
        }

        return JsonUtil.returnResult(errType,errMsg,version);

    }

    /**
     * page
     * @return
     */
    @RequestMapping("/versionListPage")
    public ModelAndView versionListPage(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/version/versionListPage");
        return modelAndView;
    }

    /**
     * 使用自定义mapper查询列表
     * @return
     */
    @RequestMapping("/getVersionDataList")
    @ResponseBody
    public Map<String,Object> getVersionDataList(TVersion reqVO,PageReq page){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        versionData(reqVO, page, dataMap);
        return dataMap;
    }

    /**
     * 列表过滤查询
     * @return
     */
    @RequestMapping("/searchVersionList")
    @ResponseBody
    public Map<String,Object> searchVersionList(TVersion reqVO, PageReq page){
        Map<String,Object> dataMap = new HashMap<String,Object>();
        versionData(reqVO, page, dataMap);
        return dataMap;
    }
    
    private void versionData(TVersion reqVO, PageReq page, Map<String, Object> dataMap) {
        List<TVersion> list = tVersionService.queryVersionList(reqVO,page);
        list = list.size() > 0 ? list : null;
        int count = tVersionService.countVersion(reqVO);
        dataMap.put("list",list);
        dataMap.put("total",count);
        dataMap.put("page",page);
    }
    
    @RequestMapping("/addVersionPage")
    public ModelAndView addVersionPage(Model model){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/version/versionAddPage");
        model.addAttribute("version",new TVersion());
        return modelAndView;
    }

    @RequestMapping("/editVersionPage")
    public ModelAndView editPersonPage(@RequestParam("id") long id, Model model){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("page/version/versionAddPage");
        TVersion tVersion = tVersionService.findVersionById(id);
        model.addAttribute("version", tVersion);
        return modelAndView;
    }

    /**
     * 修改信息
     * @param tVersion     * @return
     */
    @RequestMapping("/updateVersion")
    @ResponseBody
    public Map updateVersion(@ModelAttribute("form") TVersion tVersion) {
        String errType =ConstantEnum.CONSTANT_SUCCESS.getErrType();
        String errMsg =ConstantEnum.CONSTANT_SUCCESS.getErrMsg();
        try {
             tVersionService.updateVersion(tVersion);
        }catch (Exception e){
            errType =ConstantEnum.CONSTANT_SYS_ERROR.getErrType();
            errMsg =e.getMessage();
        }
        return JsonUtil.returnResult(errType,errMsg);
    }

    /**
     * 删除
     */
    @RequestMapping("/deleteVersion")
    @ResponseBody
    public Map deleteVersion(long id) {
        String errType =ConstantEnum.CONSTANT_SUCCESS.getErrType();
        String errMsg =ConstantEnum.CONSTANT_SUCCESS.getErrMsg();
        try {
            tVersionService.deleteVersion(id);
        }catch (Exception e){
            errType =ConstantEnum.CONSTANT_SYS_ERROR.getErrType();
            errMsg =e.getMessage();
        }
        return JsonUtil.returnResult(errType,errMsg);
    }
}